package com.yanchangapp.ui.presenter;

import android.util.Log;

import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.MenuIcons;
import com.yanchangapp.model.response.HomeBannerResponse;
import com.yanchangapp.model.response.MenuResponse;
import com.yanchangapp.model.response.WaitDoneResponse;
import com.yanchangapp.ui.view.HomeFgView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 主页View
 */
public class HomeFgPresenter extends BasePresenter<HomeFgView> {

    public HomeFgPresenter(BaseActivity context) {
        super(context);
    }

    /**
     * 查询Banner轮播图...
     */
    public void queryBannerImages(){
        HeadApiRetrofit.getInstance().queryBanner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<HomeBannerResponse>() {
                    @Override
                    public void onNext(HomeBannerResponse homeBannerResponse) {
                        List<HomeBannerResponse.DataBean> mOriginList = new ArrayList<>();
                        List<String> mImages = new ArrayList<>();
                        if (homeBannerResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (homeBannerResponse.getData() != null &&
                             homeBannerResponse.getData().size() != 0){
                                for (int i=0;i<homeBannerResponse.getData().size();i++){
                                    HomeBannerResponse.DataBean dataBean = homeBannerResponse.getData().get(i);
                                    mImages.add(dataBean.getAddress());
                                }
                                mOriginList = homeBannerResponse.getData();
                            }
                            if (getView() != null){
                                getView().getBannerSuccess(mOriginList,mImages);
                            }
                        }else {
                            if (getView() != null){
                                getView().getBannerFailure(homeBannerResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().getBannerFailure("获取轮播图失败");
                        }
                    }
                });
    }

    public void queryWaitDoneList(){
        Log.e("轮播..列表.", "queryWaitDoneList:.....todo" );
        HeadApiRetrofit.getInstance().queryWaitDoneList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<WaitDoneResponse>() {
                    @Override
                    public void onNext(WaitDoneResponse waitDoneResponse) {
                        if (waitDoneResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            List<WaitDoneResponse.DataBean> data = waitDoneResponse.getData();
                            List<WaitDoneResponse.DataBean> mList = new ArrayList<>();
                            int totalNum = 0;
                            if (data != null && data.size() != 0){
                                for (int i=0;i<data.size();i++){
                                    totalNum += data.get(i).getCount();
                                }
                                mList.addAll(data);
                            }
                            if (getView() != null){
                                getView().getWaitDoneSuccess(totalNum,mList);
                            }
                        }else {
                            if (getView() != null){
                                getView().getWaitDoneFailure(waitDoneResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().getWaitDoneFailure("获取列表失败");
                        }
                    }
                });
    }

    public void queryMenuLists(){
        HeadApiRetrofit.getInstance().queryMenuLists()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MenuResponse>() {
                    @Override
                    public void onNext(MenuResponse menuResponse) {
                        if (menuResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (getView() != null){
                                getView().getMenuListsSuccess(menuResponse.getList(),getMenusList(menuResponse.getList()));
                            }
                        }else {
                            if (getView() != null){
                                getView().getMenuListsFailure(menuResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().getMenuListsFailure("获取菜单失败");
                        }
                    }
                });
    }

    private List<MenuIcons> getMenusList(List<MenuResponse.Bean> list){
        List<MenuIcons> targetList = new ArrayList<>();
        Log.e("菜单", "getMenusList: "+list );
        if (list != null){
            for (int i=0;i<list.size();i++){
                MenuResponse.Bean bean = list.get(i);
                targetList.add(new MenuIcons(bean.getId(),bean.getName(),bean.getImageAddress(),bean.getMobileAddress()));
            }
        }
        return  targetList;
    }

}
